//
//  LoginViewController.m
//  Model
//
//  Created by apple on 2018/7/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LoginViewController.h"
#import "reviseMiMaViewController.h"//修改密码
#import "ZhuCeViewController.h"//注册

@interface LoginViewController ()
@property (nonatomic, strong) UITextField *zhangHaoTF;
@property (nonatomic, strong) UITextField *miMaTF;


@end

@implementation LoginViewController
//电池颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    UIImageView *beiJingImgView = [[UIImageView alloc] init];
    UIImage *beiJingImg = [UIImage imageNamed:@"bg_head_logo"];
    [self.view addSubview:beiJingImgView];
    [beiJingImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.offset(beiJingImg.size.height);
        
    }];
    beiJingImgView.userInteractionEnabled = YES;
    
    beiJingImgView.image =beiJingImg;
    UIButton *backButton = [[UIButton alloc] init];
    [beiJingImgView addSubview:backButton];
    
    [backButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(38);
        make.left.equalTo(self.view).with.offset(13);
        make.width.offset(screenScale(25));
        make.height.offset(screenScale(25));
    }];
    [backButton addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLab = [[UILabel alloc] init];
    [beiJingImgView addSubview:titleLab];
    titleLab.text = @"登录";
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(backButton);
    }];
    titleLab.textColor = [UIColor whiteColor];
    titleLab.font = [UIFont systemFontOfSize:17];
    
//    UIImageView *logoImgView = [[UIImageView alloc] init];
//    [self.view addSubview:logoImgView];
//    logoImgView.image = [UIImage imageNamed:@"logo_z_y"];
//    [logoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(titleLab.mas_bottom).with.offset(80);
//        make.centerX.equalTo(self.view);
//        make.width.offset(122);
//        make.height.offset(122);
//
//    }];
    
    UIImageView *zhangHaoImgView = [[UIImageView alloc] init];
    [self.view addSubview:zhangHaoImgView];
    [zhangHaoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(beiJingImgView.mas_bottom).with.offset(16);
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.height.offset(44);
    }];
    
    zhangHaoImgView.image = [UIImage imageNamed:@"line_frame"];
    zhangHaoImgView.userInteractionEnabled = YES;
    
    UIImageView *zhangHaoIcon = [[UIImageView alloc] init];
    [zhangHaoImgView addSubview:zhangHaoIcon];
    UIImage *zhangHaoIconImg =[UIImage imageNamed:@"icon_phone"];
    [zhangHaoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhangHaoImgView).with.offset(16);
        make.centerY.equalTo(zhangHaoImgView);
        make.width.offset(zhangHaoIconImg.size.width);
        make.height.offset(zhangHaoIconImg.size.height);
    }];
    zhangHaoIcon.image = [UIImage imageNamed:@"icon_phone"];
    
    UIView *zhangHaoLine = [[UIView alloc] init];
    [zhangHaoImgView addSubview:zhangHaoLine];
    [zhangHaoLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhangHaoIcon.mas_right).with.offset(8);
        make.height.offset(14);
        make.width.offset(1);
        make.centerY.equalTo(zhangHaoIcon);
        
    }];
    zhangHaoLine.backgroundColor = getColor(@"ff40c6");
    
    self.zhangHaoTF = [[UITextField alloc] init];
    [zhangHaoImgView addSubview:self.zhangHaoTF];
    [self.zhangHaoTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(zhangHaoIcon.mas_right).with.offset(17);
        make.right.equalTo(zhangHaoImgView).with.offset(-16);
        make.centerY.equalTo(zhangHaoIcon);
    }];
    self.zhangHaoTF.placeholder = @"请输入账号";
    self.zhangHaoTF.font = [UIFont systemFontOfSize:14];
    self.zhangHaoTF.textColor = getColor(@"ff40c6");
    //密码
    UIImageView *miMaImgView = [[UIImageView alloc] init];
    [self.view addSubview:miMaImgView];
    [miMaImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(zhangHaoImgView.mas_bottom).with.offset(16);
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.height.offset(44);
    }];
    
    miMaImgView.image = [UIImage imageNamed:@"line_frame"];
    miMaImgView.userInteractionEnabled = YES;
    
    UIImageView *miMaIcon = [[UIImageView alloc] init];
    [miMaImgView addSubview:miMaIcon];
    UIImage *miMaIconImg =[UIImage imageNamed:@"icon_password"];
    [miMaIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(miMaImgView).with.offset(16);
        make.centerY.equalTo(miMaImgView);
        make.width.offset(miMaIconImg.size.width);
        make.height.offset(miMaIconImg.size.height);
    }];
    miMaIcon.image = [UIImage imageNamed:@"icon_password"];
    
    UIView *miMaLine = [[UIView alloc] init];
    [miMaImgView addSubview:miMaLine];
    [miMaLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(miMaIcon.mas_right).with.offset(8);
        make.height.offset(14);
        make.width.offset(1);
        make.centerY.equalTo(miMaIcon);
        
    }];
    miMaLine.backgroundColor = getColor(@"ff40c6");
    
    self.miMaTF = [[UITextField alloc] init];
    [miMaImgView addSubview:self.miMaTF];
    [self.miMaTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(miMaIcon.mas_right).with.offset(17);
        make.right.equalTo(miMaImgView).with.offset(-16);
        make.centerY.equalTo(miMaIcon);
    }];
    self.miMaTF.placeholder = @"请输入密码";
    self.miMaTF.font = [UIFont systemFontOfSize:14];
    self.miMaTF.textColor = getColor(@"ff40c6");
    
    UIButton *phoneBtn = [[UIButton alloc] init];
    [self.view addSubview:phoneBtn];
    [phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(38);
        make.right.equalTo(self.view).with.offset(-38);
        make.top.equalTo(miMaImgView.mas_bottom).with.offset(44);
        make.height.offset(44);
    }];
    
    [phoneBtn setBackgroundImage:[UIImage imageNamed:@"b_bg1"] forState:UIControlStateNormal];
    [phoneBtn setTitle:@"登录" forState:UIControlStateNormal];
    
    [phoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    phoneBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    
    [phoneBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *huiLine = [[UIView alloc] init];
    [self.view addSubview:huiLine];
    [huiLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneBtn.mas_bottom).with.offset(13);
        make.centerX.equalTo(self.view);
        make.width.offset(1);
        make.height.offset(12);
        
    }];
    huiLine.backgroundColor = getColor(@"b8b8b8");
    
    UIButton *wangJiBtn = [[UIButton alloc] init];
    [self.view addSubview:wangJiBtn];
    [wangJiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(huiLine);
        make.right.equalTo(huiLine).with.offset(-30);
        
    }];
    [wangJiBtn setTitleColor:getColor(@"b8b8b8") forState:UIControlStateNormal];
    [wangJiBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    wangJiBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [wangJiBtn addTarget:self action:@selector(reviseClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *zhuCeBtn = [[UIButton alloc] init];
    [self.view addSubview:zhuCeBtn];
    [zhuCeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(huiLine);
        make.left.equalTo(huiLine).with.offset(30);
        
    }];
    [zhuCeBtn setTitleColor:getColor(@"b8b8b8") forState:UIControlStateNormal];
    [zhuCeBtn setTitle:@"立即注册" forState:UIControlStateNormal];
    zhuCeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    
    [zhuCeBtn addTarget:self action:@selector(zhuCeClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *xieYiLab = [[UILabel alloc] init];
    [self.view addSubview:xieYiLab];
    [xieYiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-20);
    }];
    xieYiLab.font = [UIFont systemFontOfSize:12];
    xieYiLab.textColor = getColor(@"b8b8b8");
    xieYiLab.text = @"CopyRight ©️ weqrqwerqw";
    
}
//////
- (void)leftNavItemClick{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}
/////登录
- (void)loginClick{
    
    


    
    if (![TSRegularExpressionUtil validateMobile:_zhangHaoTF.text]) {

        [PublicFunction showAlert:nil message:@"请输入正确格式的手机号码" duration:1.0];
        return;
    }

    if (![TSRegularExpressionUtil validatePassword:_miMaTF.text]) {

        [PublicFunction showAlert:nil message:@"密码为长度6-16位大小写字母和数字" duration:1.0];
        return;
    }

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;

    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _zhangHaoTF.text forKey:@"mobile"];
    [productParams setValue: _miMaTF.text forKey:@"passwd"];

    [[MNetworkHandler sharedInstance]conURL:phonePassLogin
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {

                                   if ([[returnData objectForKey:@"Status"]intValue] == 200) {

                                       [hud removeFromSuperview];
                                       [PublicFunction saveConfig:returnData[@"Data"] forKey:@"user_id"];
                                       NSLog(@"%@", [PublicFunction getConfigWithKey:@"user_id"]);
                    
                                       
                                       NSString *str =[NSString stringWithFormat:@"%@",[PublicFunction getConfigWithKey:@"user_id"]];
 
                                       NSString *encryStr = [str aci_encryptWithAES];
                                       [PublicFunction saveConfig:encryStr forKey:@"token"];
                                       NSLog(@"%@", [PublicFunction getConfigWithKey:@"token"]);
                                       MyTabBarController *barController = [[MyTabBarController alloc] init];
                                       barController.view.backgroundColor = getColor(@"ffffff");

                                       UINavigationBar *bar = [UINavigationBar appearance];

                                       [bar setBackgroundImage:[UIImage imageNamed:@"bg_top"] forBarMetrics:UIBarMetricsDefault];

                                       [bar setShadowImage:nil];
                                       bar.translucent = NO;

                                       bar.barStyle = UIBarStyleBlackTranslucent;
                                       //设置显示的颜色
                                       bar.barTintColor = getColor(@"fe45cf");
                                       //设置字体颜色
                                       bar.tintColor = [UIColor whiteColor];
                                       [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

                                       [self presentViewController:barController animated:NO completion:^{

                                       }];


                                   }else{

                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:1.0f];
                                   }

                               } failureBlock:^(NSError *error) {

                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
    
    
    
    
    
   
    
}

//////修改密码
- (void)reviseClick{
    reviseMiMaViewController *vc = [[reviseMiMaViewController alloc] init];
    
    [self presentViewController:vc animated:YES completion:^{
        
    }];
    
}
- (void)zhuCeClick{
    
    ZhuCeViewController *vc = [[ZhuCeViewController alloc] init];
    
    [self presentViewController:vc animated:YES completion:^{
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
